package com.px.chapter_Dec_1.russianGame;


import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Observable;
import java.util.Observer;

public class GamePanel extends JPanel implements Observer {
    private Shape shape;
    private GameContainer gameContainer;
    public GamePanel() {
        setBackground(new Color(0x9c9c9c));
        setSize(GameContainer.COLS*shape.SIZE,GameContainer.ROWS*Shape.SIZE);
        gameContainer=new GameContainer();
        gameContainer.addObserver(this);
        gameContainer.start();
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                switch (e.getKeyCode()){
                    case KeyEvent.VK_UP:
                        shape.rotate();
                        repaint();break;

                    case KeyEvent.VK_DOWN:
                        System.out.println("down");break;
                    case KeyEvent.VK_LEFT:
                        shape.moveLeft();
                        repaint();break;

                    case KeyEvent.VK_RIGHT:
                        shape.moveRight();
                        repaint();break;

                }
            }
        });

    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        if(shape!=null)
            drawShape(g);
    }

    private void drawShape(Graphics g) {
        g.setColor(shape.getColor());
        int left=shape.getLeft();
        int top=shape.getTop();
        int[][] data=shape.getData();
        for(int i=0;i<data.length;i++)
            for(int j=0;j<data[i].length;j++){
                if(data[i][j]==1)
                    g.fill3DRect((left+j)*shape.SIZE,(top+i)*shape.SIZE,shape.SIZE,shape.SIZE,true);
            }
    }

    @Override
    public void update(Observable o, Object arg) {
        if(arg instanceof Shape){
            shape=(Shape)arg;
            repaint();
        }
    }
}